package com.studerb.events.listener;

import static com.studerb.util.Constants.EVENT_SERVICE_NAME;

import org.apache.log4j.Logger;
import org.bushe.swing.event.annotation.AnnotationProcessor;
import org.bushe.swing.event.annotation.EventSubscriber;

import com.studerb.events.TaskServiceEvent;
import com.studerb.events.TaskServiceEvent.EventType;
import com.studerb.events.TaskServiceEvent.Queue;
import com.studerb.tasks.interfaces.ITask;
import com.studerb.view.tasks.active.ActiveTasksTableModel;

public class TaskTableTaskServiceEL {
    private final Logger logger = Logger.getLogger(TaskTableTaskServiceEL.class);
    private final ActiveTasksTableModel taskTableModel;

    public TaskTableTaskServiceEL(ActiveTasksTableModel model) {
        this.taskTableModel = model;
        AnnotationProcessor.process(this);
    }

    @EventSubscriber(eventServiceName = EVENT_SERVICE_NAME) public void onEvent(TaskServiceEvent evt) {
        ITask task = evt.getTask();
        EventType type = evt.getType();
        Queue queue = evt.getQueue();
        if(queue == Queue.ACTIVE){
            switch(type){
                case ADDED:
                    logger.debug("Task: " + task + " added to active queue");
                    taskTableModel.addTask(task);
                    break;
                case REMOVED:
                    logger.debug("Task: " + task + " removed from active queue");
                    taskTableModel.removeTask(task);
                    break;
                default:
                    break;
            }
        }
    }
}